package cn.enzo.springutilTest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.enzo.utils.utils.SqlMapper;
import cn.enzo.yun.domain.Sale;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class CreateNewFile {

	private static String tempname = "abc.ftl";
	
	public static void main(String[] args) {
		String jspTableHtml = "123";
		try {
			jspTableHtml = SqlMapper.class.newInstance().getJSPTableHtml(Sale.class);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap<String,Object> hashMap = new HashMap<String,Object>();
		hashMap.put("table", jspTableHtml);
		hashMap.put("date", new Date());
		
		createnewfile(null, "abc.jsp", hashMap);

	}

	public static void createnewfile(String temppath, String filename, Map<?, ?> map) {
		try {
			String path = System.getProperty("user.dir");
			String dir = File.separator+ "src"+ File.separator + "main" + File.separator + "webapp" +File.separator+  "temple" + File.separator; 
			Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
			cfg.setDirectoryForTemplateLoading(new File(path+ dir));
			Template template = cfg.getTemplate(tempname);
			String targerfile =  filename;
			System.out.println(targerfile);
			template.process(map, new OutputStreamWriter(new FileOutputStream(targerfile), "UTF-8"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
